package model

import (
	"database/sql"
	"fmt"
	"strings"

	"github.com/tal-tech/go-zero/core/stores/builder"
	"github.com/tal-tech/go-zero/core/stores/cache"
	"github.com/tal-tech/go-zero/core/stores/sqlc"
	"github.com/tal-tech/go-zero/core/stores/sqlx"
	"github.com/tal-tech/go-zero/core/stringx"
)

var (
	waterSoilFieldNames          = builder.RawFieldNames(&WaterSoil{})
	waterSoilRows                = strings.Join(waterSoilFieldNames, ",")
	waterSoilRowsExpectAutoSet   = strings.Join(stringx.Remove(waterSoilFieldNames, "`soilid`", "`create_time`", "`update_time`"), ",")
	waterSoilRowsWithPlaceHolder = strings.Join(stringx.Remove(waterSoilFieldNames, "`soilid`", "`create_time`", "`update_time`"), "=?,") + "=?"

	cacheMygoWaterSoilSoilidPrefix = "cache:mygo:waterSoil:soilid:"
	cacheMygoWaterSoilSoilNamePrefix = "cache:mygo:waterSoil:soilname:"
	cacheMygoWaterSoilUserNamePrefix = "cache:mygo:waterSoil:userid:"
)

type (
	WaterSoilModel interface {
		Insert(data *WaterSoil) (sql.Result, error)
		FindOne(soilid int64) (*WaterSoil, error)
		FindManyByUserId(userid int64)([]*WaterSoil,error)
		Update(data *WaterSoil) error
		Delete(soilid int64) error
		DeleteByUserName(username string) error
	}

	defaultWaterSoilModel struct {
		sqlc.CachedConn
		table string
	}

	WaterSoil struct {
		Soilid    int64  `db:"soilid"`    // 土地id
		Soilname  string `db:"soilname"`  // 土地名称
		Crop      string `db:"crop"`      // 农作物
		Soilplace string `db:"soilplace"` // 地址
		Jindu     string `db:"jindu"`     // 经度
		Weidu     string `db:"weidu"`     // 纬度
		Measure   int64  `db:"measure"`   // 土地面积
		Soiltype  string `db:"soiltype"`  // 土地类型
		Userid    int64  `db:"userid"`    // 所属农户
	}
)

func NewWaterSoilModel(conn sqlx.SqlConn, c cache.CacheConf) WaterSoilModel {
	return &defaultWaterSoilModel{
		CachedConn: sqlc.NewConn(conn, c),
		table:      "`water_soil`",
	}
}

func (m *defaultWaterSoilModel) Insert(data *WaterSoil) (sql.Result, error) {
	query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?)", m.table, waterSoilRowsExpectAutoSet)
	ret, err := m.ExecNoCache(query, data.Soilname, data.Crop, data.Soilplace, data.Jindu, data.Weidu, data.Measure, data.Soiltype, data.Userid)

	return ret, err
}

func (m *defaultWaterSoilModel) FindOne(soilid int64) (*WaterSoil, error) {
	mygoWaterSoilSoilidKey := fmt.Sprintf("%s%v", cacheMygoWaterSoilSoilidPrefix, soilid)
	var resp WaterSoil
	err := m.QueryRow(&resp, mygoWaterSoilSoilidKey, func(conn sqlx.SqlConn, v interface{}) error {
		query := fmt.Sprintf("select %s from %s where `soilid` = ? limit 1", waterSoilRows, m.table)
		return conn.QueryRow(v, query, soilid)
	})
	switch err {
	case nil:
		return &resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}



func (m *defaultWaterSoilModel) FindManyByUserId(userid int64)([]*WaterSoil,error) {

	var resp []*WaterSoil
	query := fmt.Sprintf("select %s from %s where `userid` = ? ", waterSoilRows, m.table)
	err := m.QueryRowsNoCache(&resp, query,userid)

	switch err {
	case nil:
		return resp, nil
	case sqlc.ErrNotFound:
		return nil, ErrNotFound
	default:
		return nil, err
	}
}

func (m *defaultWaterSoilModel) Update(data *WaterSoil) error {
	mygoWaterSoilSoilidKey := fmt.Sprintf("%s%v", cacheMygoWaterSoilSoilidPrefix, data.Soilid)
	_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("update %s set %s where `soilid` = ?", m.table, waterSoilRowsWithPlaceHolder)
		return conn.Exec(query, data.Soilname, data.Crop, data.Soilplace, data.Jindu, data.Weidu, data.Measure, data.Soiltype, data.Userid, data.Soilid)
	}, mygoWaterSoilSoilidKey)
	return err
}

func (m *defaultWaterSoilModel) Delete(soilid int64) error {

	mygoWaterSoilSoilidKey := fmt.Sprintf("%s%v", cacheMygoWaterSoilSoilidPrefix, soilid)
	_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("delete from %s where `soilid` = ?", m.table)
		return conn.Exec(query, soilid)
	}, mygoWaterSoilSoilidKey)
	return err
}


func (m *defaultWaterSoilModel) DeleteByUserName(username string) error {
	mygoWaterSoilSoilidKey := fmt.Sprintf("%s%v", cacheMygoWaterSoilSoilNamePrefix, username)
	_, err := m.Exec(func(conn sqlx.SqlConn) (result sql.Result, err error) {
		query := fmt.Sprintf("delete from %s where `soilname` = ?", m.table)
		return conn.Exec(query, username)
	}, mygoWaterSoilSoilidKey)
	return err
}

func (m *defaultWaterSoilModel) formatPrimary(primary interface{}) string {
	return fmt.Sprintf("%s%v", cacheMygoWaterSoilSoilidPrefix, primary)
}

func (m *defaultWaterSoilModel) queryPrimary(conn sqlx.SqlConn, v, primary interface{}) error {
	query := fmt.Sprintf("select %s from %s where `soilid` = ? limit 1", waterSoilRows, m.table)
	return conn.QueryRow(v, query, primary)
}
